from gm.api import *
import numpy as np
import talib

global google
google = []


def init(context):
    '''主程序初始化    '''
    # context.symbol = 'SZSE.300296'  # 利亚德
    # context.symbol = 'SZSE.000425'  # 徐工
    context.symbol = 'SHFE.RB'  #
    context.frequency = '1d'
    context.fields = 'open,high,low,close'
    context.volume = 1

    schedule(schedule_func=algo, date_rule='1d', time_rule='09:35:00')


def algo(context):
    now = context.now
    last_day = get_previous_trading_date('SHFE', now)
    data = history_n(symbol=context.symbol,
                     frequency=context.frequency,
                     count=124,
                     end_time=now,
                     fields=context.fields,
                     fill_missing='last',
                     adjust=ADJUST_PREV,
                     df=True,
                     )
    open = np.asarray((data['open'].values))
    high = np.asarray((data['high'].values))
    low = np.asarray((data['low'].values))
    close = np.asarray((data['close'].values))

    list_ma_5 = talib.MA(close, timeperiod=5)
    list_ma_31 = talib.MA(close, timeperiod=31)
    list_ma_123 = talib.MA(close, timeperiod=123)
    ma_5, ma_31 = list_ma_5[-1], list_ma_31[-1]

    ma_123_shang = list_ma_123[-1] - list_ma_123[-2]
    print(list_ma_31 )
    ma_31_50=list_ma_31[54:]
    # 背离
    # 连续 = ma_31[-16:]
    连涨指数_ = []
    连跌指数_ = []
    # a = 0
    # for i in 连续:
    #     if i > a:
    #         连涨指数_ += 1
    #     else:
    #         连跌指数_ += 1
    # #     a = i
    # a = 0
    # 以30为准的背离
    for i in range(1,70):
        if ma_31_50[i] - ma_31_50[i - 1] >= 0:
            连涨指数_.insert(i,ma_31_50[i])
        else:
            连涨指数_.insert(i,0)
    for i in range(1, 70):
        if ma_31_50[i] - ma_31_50[i - 1] <= 0:
            连跌指数_.append(ma_31_50[i])
        else:
            连跌指数_.append(0)
    # print(连跌指数_, 连涨指数_)
    a=0;b=0
    for i in 连涨指数_:
        if i:
            a+=1
            b+=1
        # if b

    # #
    # print(list_ma_31[31])
    chazhi = ma_31 - close[-1]
    # print(chazhi, list_ma_31[74:])

    if chazhi > 100.2:  # and ma_123_shang < -0.02:
        order_volume(symbol=context.symbol,
                     volume=context.volume,
                     side=PositionSide_Long,
                     order_type=OrderType_Market,
                     position_effect=PositionEffect_Open,
                     )
        print('买入')
    elif chazhi < -100.2:  # and ma_123_shang > 0.02:
        order_volume(symbol=context.symbol,
                     volume=context.volume,
                     side=PositionSide_Short,
                     order_type=OrderType_Market,
                     position_effect=PositionEffect_Close
                     )
        print('卖出')


if __name__ == '__main__':
    run(
        strategy_id='a5299b24-8b44-11e9-a4d4-b499baf0193a',
        filename='程序5_9不知道啥的策略.py',
        mode=MODE_BACKTEST,
        token='90be3f863b23ab3c1ef68d1f9b8dc06e4bebb30d',
        backtest_start_time='2016-06-01 09:00:00',
        backtest_end_time='2017-12-31 15:00:00',
        backtest_initial_cash=1000000,
        backtest_adjust=ADJUST_PREV,
    )
